function gray2color(grayImage) 
[M,N] = size(grayImage);
I=grayImage*128;
I2 = zeros(M, N, 3);                                 %初始化三通道

for x = 1 : M
   for y = 1 : N
      if I(x, y) <= 127                                 % R
          I2(x, y, 1) = 0;
      elseif I(x, y) <= 191
          I2(x, y, 1) = 4 * I(x, y) - 510;
      else
          I2(x, y, 1) = 255;
      end
       
      if I(x, y) <= 63                                  % G
          I2(x, y, 2) = 254 - 4 * I(x, y);
      elseif I(x, y) <= 127
          I2(x, y, 2) = 4 * I(x, y) - 254;
      elseif I(x, y) <= 191
          I2(x, y, 2) = 255;
      else
          I2(x, y, 2) = 1022 - 4 * I(x, y);
      end
       
      if I(x, y) <= 63                                 % B
          I2(x, y, 3) = 255;
      elseif I(x, y) <= 127
          I2(x, y, 3) = 510 - 4 * I(x, y);
      else
          I2(x, y, 3) = 0;
      end
   end
end
imshow(I2);
end
